import java.util.LinkedList;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class AwaitSignalStorage {
    private final int MAX_SIZE = 100;

    private LinkedList<Object> list = new LinkedList<>();

    private final Lock lock = new ReentrantLock();

    private final Condition full = lock.newCondition();
    private final Condition empty = lock.newCondition();

    public void produce(Object product) {
        lock.lock();
        while (list.size() == MAX_SIZE) {
            System.out.println("仓库已满");

            try {
                full.await();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        list.add(product);
        System.out.println("仓库已满");
        empty.signalAll();
        lock.unlock();
    }

    public Object consume() {
        lock.lock();
        while (list.size() == 0) {
            System.out.println("仓库已空");
            try {
                empty.await();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        Object res = list.remove();
        full.signalAll();
        lock.unlock();
        return res;
    }
}
